package app.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cache.CacheHelper;

import app.config.Config;
import app.dao.FileDao;
import app.dao.FixOrderDao;
import app.dao.FixOrderHandleDao;
import app.dao.FixOrderPhotoDao;
import app.domain.FileInfo;
import app.domain.FixOrder;
import app.domain.FixOrderHandle;
import app.domain.FixOrderInfo;
import app.service.FixOrderService;

@Service
public class FixOrderServiceImpl implements FixOrderService{
    
	@Autowired
	private FixOrderDao fixOrderDao;
	@Autowired
	private FixOrderPhotoDao fixOrderPhotoDao;
	@Autowired
	private FileDao fileDao;
	@Autowired
	private FixOrderHandleDao fixOrderHandleDao;
	
	@Override
	public int addFixOrder(FixOrder fixOrder) {
		fixOrderDao.addFixOrder(fixOrder);
		return fixOrder.getId();
	}
	@Override
	public List<FixOrder> getByRequire(Integer fixOrderId,Integer accountId,Integer handleStatus,Timestamp dateStart,Timestamp dateEnd) {
		return fixOrderDao.findByRequire(fixOrderId,accountId,handleStatus,dateStart,dateEnd);
	}
	@Override
	public void delete(int fixOrderId) {
		fixOrderDao.delete(fixOrderId);
	}
	@Override
	public FixOrderInfo getInfo(int fixOrderId) {
		FixOrderInfo fixOrderInfo=new FixOrderInfo();
		FixOrder fixOrder=fixOrderDao.findById(fixOrderId);
        List<Integer> fileIds=fixOrderPhotoDao.findByFixOrderId(fixOrderId);
        List<FileInfo> photoList=new ArrayList<>();
        for(Integer fileId:fileIds){
        	photoList.add(fileDao.findById(fileId));
        }
        
        FixOrderHandle fixOrderHandle=fixOrderHandleDao.findByFixOrderId(fixOrderId);
        
        fixOrderInfo.setFixOrder(fixOrder);
        fixOrderInfo.setPhotoList(photoList);
        
        fixOrderInfo.setFixOrderHandle(fixOrderHandle);
        return fixOrderInfo;
	}
	@Override
	public void updateInfo(FixOrder fixOrder) {
		fixOrderDao.update(fixOrder);
	}
	@Override
	public void closeOrder(int id) {
		fixOrderDao.updateHandleStatus(id, Config.FIXORDER_HANDLE_STATUS_FINISH);
		fixOrderDao.updateFinishTime(id,new Timestamp(new java.util.Date().getTime()));
	}
	@Override
	public void updateHandleStatus(int fixOrderId, int handleStatus) {
		fixOrderDao.updateHandleStatus(fixOrderId, handleStatus);
	}
	
	@Override
	public List<FixOrder> getByRequire(Integer workerId,Integer fixOrderId,Integer handleStatus) {
		return fixOrderDao.findWorkerRequire(workerId,fixOrderId,handleStatus);
	}
	
	@Override
	public Integer findByTime(final String format, final String format2) {
		String key = "findByTime"+format+format2;
		Callable<Integer> fixCacheLoader = new Callable<Integer>() {

			public Integer call() throws Exception {
				System.out.println("via db");
				return fixOrderDao.findByTime(format, format2);
			}
		};
		try {
			return CacheHelper.get(key,fixCacheLoader);
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	@Override
	public Integer findByTimeForWork(final String format, final String format2) {
		// TODO Auto-generated method stub
		String key = "findByTimeForWork"+format+format2;
		Callable<Integer> fixCacheLoader = new Callable<Integer>() {

			public Integer call() throws Exception {
				System.out.println("via db2");
				return fixOrderDao.findByTimeForWork(format, format2);
			}
		};
		try {
			return CacheHelper.get(key,fixCacheLoader);
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;		

	}

}
